package com.dyz.wsw.front.market.service.impl;

import com.dyz.wsw.front.market.config.MarketProperties;
import com.dyz.wsw.front.market.dao.persist.repository.MarketCardRepository;
import com.dyz.wsw.front.market.pojo.entity.MarketCard;
import com.dyz.wsw.front.market.service.CardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
import org.springframework.data.relational.core.query.Criteria;
import org.springframework.data.relational.core.query.Query;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @author dyz
 * @date 2025/1/15 0:14
 */
@Service
@Slf4j
public class CardServiceImpl implements CardService{

    private final MarketProperties marketProperties;

    private final MarketCardRepository marketCardRepository;

    private final R2dbcEntityTemplate template;

    public CardServiceImpl(
            MarketCardRepository marketCardRepository,
            R2dbcEntityTemplate template,
            MarketProperties marketProperties
            ) {
        log.debug("创建业务类对象: CardServiceImpl");
        this.marketCardRepository = marketCardRepository;
        this.template = template;
        this.marketProperties = marketProperties;
    }

    @Override
    public Flux<MarketCard> getRandCards(Integer count) {

        log.debug("开始处理【获取指定数量的随即卡片】的业务，数量：{}",count);
        return marketCardRepository.findRandCards(count);
    }

    @Override
    public Flux<MarketCard> getMoreCards(Long cid,Integer skipSize,Integer requestSize) {
        log.debug("开始处理【请求更多卡片数据】的业务,卡片目录: {},跳过条数: {},请求条数: {}",cid,skipSize,requestSize);
        Query query;
        if(cid==null){
            query = Query.query(Criteria.empty());
        }else {
            query = Query.query(Criteria.where("c_id").is(cid));
        }
        query = query.limit(requestSize).offset(skipSize);

        return template.select(MarketCard.class)
                .matching(query)
                .all();
    }

    @Override
    public Mono<Long> getCardsCount(Long cid) {
        log.debug("开始处理【请求获取卡片总数量】的业务,cid: {}",cid);
        Query query;
        if(cid==null){
            query = Query.query(Criteria.empty());
        }else {
            query = Query.query(Criteria.where("c_id").is(cid));
        }
        return template.select(MarketCard.class)
                .matching(query).count();
    }

    @Override
    public Flux<MarketCard> getCardsByPage(Long cid, Integer currentPage, Integer requestCount) {
        log.debug("开始处理【获取指定页指定数量卡片】的业务,cid: {},currentPage: {},requestCount: {}",cid,currentPage,requestCount);
        Query query;
        if(cid==null){
            query = Query.query(Criteria.empty());
        }else {
            query = Query.query(Criteria.where("c_id").is(cid));
        }
        query = query.limit(requestCount).offset((long) (currentPage - 1) * marketProperties.getPageSize());
        return template.select(MarketCard.class)
                .matching(query)
                .all();
    }
}
